<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				InstCacheKey: "fin-js-hourWage-inst-cache",
				initValue:{
					addProductProcess:false
				}
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			formSubmit(){
				let _that = this
				if(_that.edit){
					_that.om.formSave("pieceRateWage/doUpdate",_that.formdata,_that)
				}else{
					_that.om.formSave("pieceRateWage/doAdd",_that.formdata,_that)
				}
			},
			verifyDataStr(){
				let _that = this
				if(_that.$refs.autoform.formdata.instId){
					return true
				}else{
					_that.om.toast('请先选择部门名称',3000)
					return false
				}
			},
			intLoad(options){
				let _that = this
				let fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code)
				]
				if(fileId){
					ajaxArr.push({
						url:"pieceRateWage/getValue",
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
				changeEditor("topinfo", "zposId", "data", configSelectData(res[0], "id", "posname"), mainData)
				changeEditor('title0', 'instId', {
					link: "../../public/pageSearch/pageSearch",
					"linkParam": {
						other: JSON.stringify({
							containBoard: true
						}),
						method: "post",
						namefield: 'instName',
						searchfield: "instName",
						initload: true,
						url: 'tinstitution/findNotBoard1',
						cachename: _that.InstCacheKey,
						placeholder: "请输入部门名称"
					}
				}, null, mainData)
				if(_that.subEditorObj["title1"]){
					changeEditor("title1","dataStr",{
						titleTemplate: "产品编码:<%=projectCode||'未填写'  %>",
						subtitle1Template: "产品名称:<%=projectName ||'未填写'%>",
						subtitle2Template:"规格型号:<%=  projectStd||'未填写' %>"
					},null,mainData)
				}
				if(_that.subEditorObj["title2"]){
					changeEditor("title2","reviewerStr",{
						titleTemplate:"姓名:<%= userName||'未填写'%>",
						subtitle1Template:"岗位:<%= postName||'未填写' %>"
					},null,mainData)
				}
				changeConfigAttr("title1","showButton",true,mainData)
				changeConfigAttr("title1","buttonText","新增工序",mainData)
				changeConfigAttr("title1","buttonLink","/pages/form/productProcess/productProcess",mainData)
				if(fileId){
					let fieldArr = getFormFields(mainData)
					fieldArr.push("id")
					for(let index in res[1].value){
						if(fieldArr.includes(index)){
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value[index])
							_that.$set(_that.formdata,index,res[1].value[index])
						}
					}
					let dataList = res[1].value.dataList
					_that.subEditorObj["title1"].workList = dataList
					_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataStr",JSON.stringify(dataList))
					_that.$set(_that.formdata,"dataStr",JSON.stringify(dataList))
					delete res[1].value.dataList
				}
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.intLoad(options)
		},
		onUnload(){
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow() {
			let _that = this
			if(_that.addProductProcess){
				_that.om.toast("保存成功",3000)
				_that.addProductProcess = false
			}
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					let editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.InstCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							if(!formdata.instId && !formdata.instName){
								formdata.instId = d.id
								formdata.instName = d.instName
							}else{
								if(formdata.instId != d.id && formdata.instName != d.instName){
									formdata.instId = d.id
									formdata.instName = d.instName
									_that.subEditorObj["title1"].workList = ""
								}
							}
							_that.om.request(
								"tposition/getPrimary",
								{
									instId:d.id
								}
							).then(res=>{
								if(res.value){
									formdata.instUserId = res.value.userId
									formdata.instUserName = res.value.userName
								}
							})
							uni.removeStorage({
								key:_that.InstCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
